<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="250" :formType="type" :isSubmitBtn="false" @formChange="formChangeHandle" />
    <div
      :style="{
        textAlign: 'center'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button v-if="type !== 'show'" type="primary" @click="saveDialog">{{ btnText }}</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { findAllRepair, updateBasicParam, findStorageCode } from '@/api/repair/basicParameters';
import { getStroageList } from '@/api/part/partOutbound/partBreak';
export default {
  name: 'PartModal',
  mixins: [dictionary],
  props: {
    type: {
      type: String,
      default: 'add'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改', show: '明细' };
    this.saveData = null;

    return {
      formList: this.createFormList()
    };
  },
  computed: {
    btnText() {
      return this.conf[this.type];
    }
  },
  created() {
    this.init();
    this.getStorage();
  },
  mounted() {
    this.queryDefaultPa();
  },
  methods: {
    init() {
      if (this.type !== 'add') {
        // 更新接口需要的记录 ID
        this.recordId = this.rowData.partNo;
        this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
        });
      }
    },
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '缺省仓库',
          fieldName: '1100',
          placeholder: '请选择',
          rules: [{ required: true, message: '请输入缺省仓库', trigger: 'blur' }],
          itemList: []
        },
        {
          type: 'CHECKBOX',
          label: '是否自动打印领料单',
          fieldName: '9103'
        },
        // {
        //   type: 'CHECKBOX',
        //   label: '仓库关单',
        //   fieldName: '3115'
        // },
        {
          type: 'SELECT',
          label: '内部领用价格类型',
          fieldName: '1061',
          placeholder: '请选择',
          rules: [{ required: true, message: '请输入内部领用价格类型', trigger: 'blur' }],
          itemList: this.createDictList('8050')
        },
        {
          type: 'CHECKBOX',
          label: '领料时必须指定领料人',
          fieldName: '1023'
        },
        {
          type: 'SELECT',
          label: '领料打印显示顺序',
          fieldName: '8551',
          placeholder: '请选择',
          rules: [{ required: true, message: '请输入领料打印显示顺序', trigger: 'blur' }],
          itemList: this.createDictList('8056')
        },
        {
          type: 'CHECKBOX',
          label: '领料单打印配件库存',
          fieldName: '2015'
        }
        // {
        //   type: 'INPUT',
        //   label: '维修税率',
        //   fieldName: '1003'
        // },
      ];
    },
    async getStorage() {
      const res = await getStroageList();
      this.formList[0].itemList = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
    },
    formChangeHandle(val) {
      this.saveData = val;
    },

    async saveDialog() {
      if (this.formList.find(x => x.fieldName === '1100').initialValue == '' || this.formList.find(x => x.fieldName === '1100').initialValue == null) {
        return this.$notify.warning({ title: '提示信息', message: '缺省仓库不能为空' });
      }
      if (this.formList.find(x => x.fieldName === '1061').initialValue == '' || this.formList.find(x => x.fieldName === '1061').initialValue == null) {
        return this.$notify.warning({ title: '提示信息', message: '内部领用价格类型不能为空' });
      }
      if (this.formList.find(x => x.fieldName === '8551').initialValue == '' || this.formList.find(x => x.fieldName === '8551').initialValue == null) {
        return this.$notify.warning({ title: '提示信息', message: '领料打印显示顺序不能为空' });
      }
      this.$refs.formPanel.SUBMIT_FORM();
      let res = await updateBasicParam(this.saveData);
      if (res.resultCode === 200) {
        this.test();
        this.closeDialog();
      }
    },
    async queryDefaultPa() {
      let res = await findAllRepair();

      // for (let i = 0; i < this.formList[0].itemList.length; i++) {
      //   if (this.formList[0].itemList[i].value == res.data['1100']) {
      //     this.formList[0].initialValue = this.formList[0].itemList[i].value;
      //   }
      // }
      this.formList.find(x => x.fieldName === '1100').initialValue = res.data['1100'];
      this.formList.find(x => x.fieldName === '9103').initialValue = res.data['9103'];
      this.formList.find(x => x.fieldName === '1061').initialValue = +res.data['1061'];
      this.formList.find(x => x.fieldName === '1023').initialValue = res.data['1023'];
      this.formList.find(x => x.fieldName === '8551').initialValue = +res.data['8551'];
      this.formList.find(x => x.fieldName === '2015').initialValue = res.data['2015'];

      // this.formList[1].initialValue = res.data['3115'];
      // this.formList[2].initialValue = +res.data['1061'];
      // this.formList[3].initialValue = res.data['1023'];
      // this.formList[4].initialValue = +res.data['8551'];
      // this.formList[5].initialValue = res.data['2015'];
      // //this.formList[6].initialValue = res.data['1003'];
      // this.formList[6].initialValue = res.data['9103'];
    },

    closeDialog(param) {
      this.$refs.formPanel.RESET_FORM();
      this.queryDefaultPa();
    },
    test() {
      this.$notify({
        title: '成功',
        message: '成功',
        type: 'success'
      });
    }
  }
};
</script>

<style lang="less" scoped>
p {
  background-color: #d3dce6;
  height: 20px;
  line-height: 20px;
  margin-bottom: 10px;
}
.elspan {
  margin-bottom: 70px;
}
span {
  top: 50px;
  color: red;
}

.el-row {
  margin-bottom: 50px;
}
</style>
